package com.czf.company.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.czf.common.entity.BankUserInfo;
import com.czf.common.entity.RecordUserBank;
import com.czf.common.mapper.BankUserInfoMapper;
import com.czf.common.mapper.RecordUserBankMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author zfChen
 * @create 2022/4/25 10:54
 */
@Service
public class BankInfoServiceImpl {

    @Autowired
    private BankUserInfoMapper bankUserInfoMapper;
    @Autowired
    private RecordUserBankMapper recordUserBankMapper;

    public int select(BankUserInfo bankUserInfo, Long userId) {
        QueryWrapper<BankUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("card_num", bankUserInfo.getCardNum());
        wrapper.eq("bank_name", bankUserInfo.getBankName());
        wrapper.eq("bank_user", bankUserInfo.getBankUser());
        wrapper.eq("telphone", bankUserInfo.getTelphone());
        BankUserInfo info = bankUserInfoMapper.selectOne(wrapper);
        if (ObjectUtils.isNotEmpty(info)) {
            QueryWrapper<RecordUserBank> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",userId);
            queryWrapper.eq("bank_name",bankUserInfo.getBankName());
            RecordUserBank bank = recordUserBankMapper.selectOne(queryWrapper);
            if (ObjectUtils.isNotEmpty(bank)) {
                if (info.getSituation() == 0) {
                    bank.setBadBank(1);
                    bank.setGoodBank(0);
                }else if (info.getSituation() == 1) {
                    bank.setGoodBank(1);
                    bank.setBadBank(0);
                }
                else if (info.getSituation() == 2) {
                    bank.setGoodBank(2);
                    bank.setBadBank(0);
                }
                UpdateWrapper<RecordUserBank> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("user_id",userId);
                updateWrapper.eq("bank_name",bankUserInfo.getBankName());
                recordUserBankMapper.update(bank,updateWrapper);
            } else {
                RecordUserBank recordUserBank = new RecordUserBank();
                recordUserBank.setUserId(userId);
                recordUserBank.setBankName(bankUserInfo.getBankName());
                if (info.getSituation() == 0) {
                    recordUserBank.setBadBank(1);
                }
                if (info.getSituation() == 1) {
                    recordUserBank.setGoodBank(1);
                }
                if (info.getSituation() == 2) {
                    recordUserBank.setGoodBank(2);
                }
                recordUserBankMapper.insert(recordUserBank);
            }
            return info.getSituation();
        }
        return 10;
    }

    public int getBankScore(Long userId) {
        QueryWrapper<RecordUserBank> wrapper = new QueryWrapper<>();
        wrapper.select("SUM(good_bank) as goodNum,SUM(bad_bank) as badNum");
        wrapper.eq("user_id", userId);
        List<Map<String, Object>> maps = recordUserBankMapper.selectMaps(wrapper);
        Map<String, Object> map = maps.get(0);
        int good = 0;
        int bad = 0;
        if (ObjectUtils.isNotEmpty(map)) {
            if (ObjectUtils.isNotEmpty(map.get("goodNum"))) {
                good = Integer.parseInt(map.get("goodNum").toString());
            }
            if (ObjectUtils.isNotEmpty(map.get("badNum"))) {
                bad = Integer.parseInt(map.get("badNum").toString());
            }
        }

        if (bad >= 2) {
            return 0;
        } else if (bad == 0 && good >= 6) {
            return 100;
        } else if (bad == 0 && good >= 2 && good <= 5) {
            return 50 + 5 * good;
        } else if (good >= 6) {
            BigDecimal b = new BigDecimal((float) (good - 2 * bad) / good);
            Double result = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            return (int) (result * 100);
        }

        return 0;
    }
}
